package com.zgl.gemduel.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // ✅ 1. 保留你现有的 SockJS 端点（用于网页测试）
        registry.addEndpoint("/ws")
                .setAllowedOriginPatterns("*")  // 允许跨域
                .withSockJS(); // 启用 SockJS 降级支持

        // ✅ 2. 新增：原生 WebSocket 端点（用于 Taro 小程序/H5）
        registry.addEndpoint("/ws-native")
                .setAllowedOriginPatterns("*"); // 不加 .withSockJS() → 原生 WebSocket
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 启用简单消息代理，订阅 /topic 或 /queue 开头的消息
        registry.enableSimpleBroker("/topic", "/queue", "/user");
        // 客户端发送消息的前缀，如 /app/join_room
        registry.setApplicationDestinationPrefixes("/app");
        // 用户对用户消息前缀
        registry.setUserDestinationPrefix("/user");
    }
}